using System.IO;
using System.Text;

namespace Dojo {
    public class ResolvedUrl {
        private string url;
        private const string DOJO = "dojo";
        
        public override string ToString() {
            return this.url;
        }

        public bool IsFile {
            get { return !url.EndsWith("*"); }
        }

        public string DojoPath {
            get { return IsFile ? filePath() : directoryPath(); }
        }

        private string directoryPath() {
            StringBuilder result = new StringBuilder(this.url);
            result.Replace(DOJO, DOJO + Path.DirectorySeparatorChar + "src");
            result.Replace('.', Path.DirectorySeparatorChar);
            result.Replace("*","");
            return result.ToString();
        }

        private string filePath() {
            StringBuilder result = new StringBuilder(this.url);
            result.Replace(DOJO, DOJO + Path.DirectorySeparatorChar + "src");
            result.Replace('.', Path.DirectorySeparatorChar);
            result.Append(".js");
            return result.ToString();
        }

        public string DirectoryPartOfFilePath {
            get
            {
                StringBuilder result = new StringBuilder(this.url.Substring(0, this.url.LastIndexOf('.')));
                result.Replace(DOJO, DOJO + Path.DirectorySeparatorChar + "src");
                result.Replace('.', Path.DirectorySeparatorChar);
                return result.ToString();
            }
        }

        public bool IsDirectory {
            get { return url.EndsWith("*"); }
        }

        public string Url {
            get { return this.url; }
        }

        public override int GetHashCode() {
            return url.GetHashCode();
        }

        public override bool Equals(object obj) {
            if (this == obj) return true;
            ResolvedUrl resolvedUrl = obj as ResolvedUrl;
            if (resolvedUrl == null) return false;
            if (!Equals(url, resolvedUrl.url)) return false;
            return true;
        }

        public ResolvedUrl(string url) {
            this.url = url;
        }
    }
}